﻿using Commons.ASPNETCore;
using FileService.Domain;
using FileService.Domain.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FileService.WebApi.Controllers
{
    [Route("BaseVideo/[controller]")]
    [ApiController]
    public class TimeTabController : ControllerBase
    {
        private readonly ITabRepository repository;
        public TimeTabController(ITabRepository repository)
        {
            this.repository = repository;
        }


        /// <summary>
        /// 获取子列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> QueryList()
        {
            var list = await repository.QueryTimeTabAsync();
            return Result.Success(list);
        }

        [HttpGet("{id}")]
        public async Task<Result> Get(long id)
        {
            var a = await repository.FindByIdAsync<TimeTab>(id);
            return Result.Success(a);
        }

        [HttpPost]
        public async Task<Result> Add([FromBody] string name)
        {
            var re = await repository.FindTimeTabByNameAsync(name);
            if (re != null)
            {
                return Result.Fail(CodeType.HAVE_DATA);
            }

            var result = await repository.AddAsync(TimeTab.Create(name));
            if (result == 0)
            {
                return Result.Fail();
            }
            return Result.Success();
        }

        [HttpDelete]
        public async Task<Result> Delete([FromBody] TimeTab tab)
        {
            var re = await repository.DeleteAsync(tab);
            return Result.Success(re);
        }

        [HttpPut]
        public async Task<Result> Put([FromBody] TimeTab tab)
        {
            var re = await repository.UpdateAsync(tab.UpdataTime());
            return Result.Success(re);
        }
    }
}
